import requests
import datetime
from pyquery import PyQuery as pq
import json
# // 抓取节气的脚本
url = 'https://jieqi.bmcx.com/2024__jieqi/'
res = requests.get(url).text
lists = pq(res)

itms = lists('.jieqi_k_sj').items('strong')
dates = lists('.jieqi_k_sj').items('span')

collectedData = {
    'code': 200,
    'data': []
}
holiday_or_cn = []
for itm in itms:
    holiday_or_cn.append(itm.text())

date = []
today = datetime.datetime.today()
# 可自定义年份
year = str(2024)
for itm in dates:
    D = str(itm.text())
    date.append(int(year + D[0:2] + D[3:5]))

for index, itm in enumerate(holiday_or_cn):
    itemData = {
        'date': date[index],
        'holiday_or_cn': holiday_or_cn[index]
    }
    collectedData['data'].append(itemData)

print(collectedData)
jsonData = json.dumps(collectedData, ensure_ascii=False, indent=2)
jsonFile = open('solarTerms.json', 'w', encoding='utf-8')
jsonFile.write(jsonData)
jsonFile.close()
